package com.yanqu.road.server.manager.activity.QixiCrossMgr;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.qixi.QixiConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.peakseacraft.PeakSeacraftConfigMgr;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossQixiConfigMgr extends TempCrossMgr {
    private static Logger logger = LogManager.getLogger(CrossQixiConfigMgr.class.getName());
    private static Map<Integer, QixiConfig> qixiConfigMap = new ConcurrentHashMap<>();

    public static QixiConfig getConfig(int activityId) {
        return qixiConfigMap.get(activityId);
    }

    public static void removeConfig(int activityId) {
        qixiConfigMap.remove(activityId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadActivityConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    public static synchronized void reloadActivityConfig() {
        logger.info("reload CrossQixiConfigMgr start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.QiXiActivity.getValue());

        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadConfig(item);
        }
        logger.info("reload CrossQixiConfigMgr end");
    }

    public static synchronized QixiConfig reloadConfig(ActivityInfo activityInfo) {
        List<Integer> activityIdList = new ArrayList<>();
        int activityId = activityInfo.getActivityId();
        activityIdList.add(activityId);
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);

        QixiConfig config = new QixiConfig(activityInfo, activityConfigMap);
        qixiConfigMap.put(activityId, config);

        return config;
    }
}
